package com.woniu.micropropertyserver.parkmag.controller;

import com.github.pagehelper.PageInfo;
import com.woniu.microcommonsserver.entity.*;
import com.woniu.microcommonsserver.param.PageRequest;
import com.woniu.microcommonsserver.param.ResponseMsg;
import com.woniu.microcommonsserver.param.TableResult;
import com.woniu.micropropertyserver.bean.PostAndString;
import com.woniu.micropropertyserver.bean.UnBindJson;
import com.woniu.micropropertyserver.housemag.service.impl.HouseServiceImpl;
import com.woniu.micropropertyserver.parkmag.service.impl.ParkServiceImpl;
import com.woniu.micropropertyserver.service.impl.OwnerInfoServiceImpl;
import com.woniu.micropropertyserver.service.impl.RelativesInfoServiceImpl;
import com.woniu.micropropertyserver.service.impl.RenterInfoServiceImpl;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/park")
public class ParkController {

    @Resource
    private ParkServiceImpl parkService;

    @Resource
    private OwnerInfoServiceImpl ownerInfoService;

    @Resource
    private RelativesInfoServiceImpl relativesInfoService;

    @Resource
    private RenterInfoServiceImpl renterInfoService;

    @GetMapping(value = "/findPark",produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseMsg findPark(){
        List<ParkInfoEntity> list = parkService.findPark();
        return ResponseMsg.builder().code(200).msg("查询成功").data(list).build();
    }

    @RequestMapping(value = "/findBy",produces = {"application/json;charset=utf-8"})
    public ResponseMsg findBy(PageRequest request,ParkCarInfoEntity parkCarInfoEntity){
        Integer sNum = request.getOffset();
        Integer lNum = request.getLimit();
        Integer pNum = request.getPageNum();
        if(sNum == null || sNum < 0){
            sNum = 0;
            request.setOffset(sNum);
        }
        if(lNum == null || lNum <= 0){
            lNum = 10;
            request.setLimit(lNum);
        }
        if(pNum == null || pNum <= 0){
            pNum = 1;
            request.setPageNum(pNum);
        }
        PageInfo<ParkCarInfoEntity> pageInfo = parkService.findBy(request,parkCarInfoEntity);
        TableResult list = new TableResult<ParkCarInfoEntity>(pageInfo.getTotal(),pageInfo.getList());
        return ResponseMsg.builder().code(200).msg("查询成功").data(list).build();
    }

    @GetMapping(value = "/findChange",produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseMsg findChange(String id){
        List<ParkcarChangeInfoEntity> list = parkService.findChange(id);
        return ResponseMsg.builder().code(200).msg("查询成功").data(list).build();
    }

    @GetMapping(value = "/findBuCard",produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseMsg findBuCard(String id){
        OwnerInfoEntity o = ownerInfoService.findByCard(id);
        return ResponseMsg.builder().code(200).msg("查询成功").data(o).build();
    }

    @GetMapping(value = "/findRelCard",produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseMsg findRelCard(String id){
        RelativesInfoEntity o = relativesInfoService.findByCard(id);
        return ResponseMsg.builder().code(200).msg("查询成功").data(o).build();
    }

    @GetMapping(value = "/findRenCard",produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseMsg findRenCard(String id){
        RenterInfoEntity o = renterInfoService.findByCard(id);
        return ResponseMsg.builder().code(200).msg("查询成功").data(o).build();
    }

    @PostMapping(value = "/bind",produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseMsg bind(@RequestBody PostAndString postAndString){
        parkService.bind(postAndString.getParkCarInfoEntity());
        ParkcarChangeInfoEntity parkcarChangeInfoEntity = new ParkcarChangeInfoEntity();
        parkcarChangeInfoEntity.setFkPcId(postAndString.getParkCarInfoEntity().getId());
        parkcarChangeInfoEntity.setDochange("业主绑定，新业主："+postAndString.getParkCarInfoEntity().getUsername());
        parkcarChangeInfoEntity.setDoexplain("车位出售");
        parkcarChangeInfoEntity.setUser(postAndString.getDoer());
        parkcarChangeInfoEntity.setChangetime(new Date());
        boolean x = parkService.addChange(parkcarChangeInfoEntity);
        return ResponseMsg.builder().code(200).msg("绑定成功").data(x).build();
    }

    @PostMapping(value = "/unbind",produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseMsg unbind(@RequestBody UnBindJson unBindJson){
        parkService.unbind(unBindJson.getHid());
        ParkcarChangeInfoEntity parkcarChangeInfoEntity = new ParkcarChangeInfoEntity();
        parkcarChangeInfoEntity.setFkPcId(Long.valueOf(unBindJson.getHid()));
        parkcarChangeInfoEntity.setDochange("业主取绑，原业主："+ unBindJson.getUser());
        parkcarChangeInfoEntity.setUser(unBindJson.getDoer());
        parkcarChangeInfoEntity.setChangetime(new Date());
        boolean x = parkService.addChange(parkcarChangeInfoEntity);
        return ResponseMsg.builder().code(200).msg("取绑成功").data(x).build();
    }

    @PostMapping(value = "/cBind",produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseMsg cBind(@RequestBody PostAndString postAndString){
        parkService.cBind(postAndString.getParkCarInfoEntity());
        ParkcarChangeInfoEntity parkcarChangeInfoEntity = new ParkcarChangeInfoEntity();
        parkcarChangeInfoEntity.setFkPcId(postAndString.getParkCarInfoEntity().getId());
        parkcarChangeInfoEntity.setDochange("业主换绑，新业主："+postAndString.getParkCarInfoEntity().getUsername());
        parkcarChangeInfoEntity.setUser(postAndString.getDoer());
        parkcarChangeInfoEntity.setChangetime(new Date());
        boolean x = parkService.addChange(parkcarChangeInfoEntity);
        return ResponseMsg.builder().code(200).msg("换绑成功").data(x).build();
    }

    @PostMapping(value = "/rent",produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseMsg rent(@RequestBody PostAndString postAndString){
        parkService.rent(postAndString.getParkCarInfoEntity());
        ParkcarChangeInfoEntity parkcarChangeInfoEntity = new ParkcarChangeInfoEntity();
        parkcarChangeInfoEntity.setFkPcId(postAndString.getParkCarInfoEntity().getId());
        parkcarChangeInfoEntity.setDochange("车位租贷，新租户："+postAndString.getParkCarInfoEntity().getUsername());
        parkcarChangeInfoEntity.setDoexplain(postAndString.getExplain());
        parkcarChangeInfoEntity.setUser(postAndString.getDoer());
        parkcarChangeInfoEntity.setChangetime(new Date());
        boolean x = parkService.addChange(parkcarChangeInfoEntity);
        return ResponseMsg.builder().code(200).msg("租贷成功").data(x).build();
    }

    @PostMapping(value = "/unrent",produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseMsg unrent(@RequestBody UnBindJson unBindJson){
        parkService.unbind(unBindJson.getHid());
        ParkcarChangeInfoEntity parkcarChangeInfoEntity = new ParkcarChangeInfoEntity();
        parkcarChangeInfoEntity.setFkPcId(Long.valueOf(unBindJson.getHid()));
        parkcarChangeInfoEntity.setDochange("取消租贷，原租户："+unBindJson.getUser());
        parkcarChangeInfoEntity.setUser(unBindJson.getDoer());
        parkcarChangeInfoEntity.setChangetime(new Date());
        boolean x = parkService.addChange(parkcarChangeInfoEntity);
        return ResponseMsg.builder().code(200).msg("取租成功").data(x).build();
    }

    @PostMapping(value = "/cRent",produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseMsg cRent(@RequestBody PostAndString postAndString){
        parkService.cRent(postAndString.getParkCarInfoEntity());
        ParkcarChangeInfoEntity parkcarChangeInfoEntity = new ParkcarChangeInfoEntity();
        parkcarChangeInfoEntity.setFkPcId(postAndString.getParkCarInfoEntity().getId());
        parkcarChangeInfoEntity.setDochange("车位调整，新租户："+postAndString.getParkCarInfoEntity().getUsername());
        parkcarChangeInfoEntity.setDoexplain(postAndString.getExplain());
        parkcarChangeInfoEntity.setUser(postAndString.getDoer());
        parkcarChangeInfoEntity.setChangetime(new Date());
        boolean x = parkService.addChange(parkcarChangeInfoEntity);
        return ResponseMsg.builder().code(200).msg("调整成功").data(x).build();
    }


}
